package com.zhixiang.wbl.visitor.Helper;

import android.content.Context;
import android.util.Log;

import com.snappydb.DB;
import com.snappydb.DBFactory;
import com.snappydb.SnappydbException;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhixiang.wbl.visitor.Constant.Constant;
import com.zhixiang.wbl.visitor.Utils.URLUtil;
import com.zhixiang.wbl.visitor.model.MyUserInfo;
import com.zhixiang.wbl.visitor.model.RootMyUserInfo;

import java.util.HashMap;
import java.util.Map;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 * Created by Administrator on 2016/7/4.
 */
public class MyUserInfoProvider implements RongIM.UserInfoProvider
{
    private static final String TAG = "MyUserInfoProvider";
    private  Context mContext;

//    private static final String USER_INFO_PRE = "USER_INFO_PRE";
//    private static final String dbName = "Userinfo";
    public MyUserInfoProvider(Context mContext)
    {
        this.mContext = mContext;
    }

    @Override
    public UserInfo getUserInfo(final String s)
    {
       // Log.e("MyUserInfoProvider", "getUserInfo: " );
        // 1先获取当前的缓存，如果缓存中不存在，则从网络中获取
        UserInfo userinfo = getUserinfoFromCache(s);
        if (userinfo!=null) return userinfo;
        //2如果缓存中不存在，则从网络中加载
        OkHttpHelper helper = OkHttpHelper.getInstance();
        Map<String,String> map = new HashMap<>();
        map.put("targetId",s);
        String url = URLUtil.getURL(Constant.UserInfo_GET_URL,map);
        helper.get(url, new BaseCallback<RootMyUserInfo>() {
            @Override
            public void onBeforeRequest(Request request) {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onResponse(Response response) {

            }

            @Override
            public void onSuccess(Response response, RootMyUserInfo userinfo)
            {
                if (200==userinfo.getCode())
                {

                    cacheUserInfo(s,userinfo.getData());
                    //Log.e("MyUserInfoProvider", "onSuccess cache: "+userinfo );
                    RongIM.getInstance().refreshUserInfoCache(userinfo.getData().getUserInfo());
                }

            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
        return null;
    }
    /*
    * 从缓存中读取用户当前的Userinfo
    * */
    public  UserInfo getUserinfoFromCache(String s)
    {
         String userKey = s;
        try {
            DB snapyDB = DBFactory.open(mContext);//,Constant.USERINFO_dbName
            MyUserInfo userinfo = snapyDB.get(userKey, MyUserInfo.class);
            if (userinfo!=null) {Log.e("MyUserInfoProvider", "getUserinfoFromCache: "+userinfo );return userinfo.getUserInfo();}
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    * 缓存Userinfo
    * */
    public void cacheUserInfo(String s,MyUserInfo userInfo)
    {
        String userKey = s;
        try {
            DB snapyDB = DBFactory.open(mContext);//,Constant.USERINFO_dbName
            snapyDB.put(userKey,userInfo);
            Log.e(TAG, "cacheUserInfo: "+getUserinfoFromCache(s));
        } catch (SnappydbException e) {
            e.printStackTrace();
        }

    }
}
